﻿using FirstFloor.ModernUI.Windows.Controls;
using System;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using Vtron.HipcConfigurationTools.Core;
using Vtron.HipcConfigurationTools.Domain;

namespace Vtron.HipcConfigurationTools.VWASStyle
{
    /// <summary>
    /// SetRTCWindow.xaml 的交互逻辑
    /// </summary>
    public partial class SetTimeWindow : ModernWindow
    {
        public SetTimeWindow(bool ntpEnable)
        {
            InitializeComponent();

            this.Style = (Style)App.Current.Resources["EmptyWindow"];
            Loaded += SetTimeWindow_Loaded;
            ntp_RadioButton.IsChecked = ntpEnable;
        }

        private void SetTimeWindow_Loaded(object sender, RoutedEventArgs e)
        {
            for (int i = 1; i < 25; i++)
            {
                ComboBoxItem item = new ComboBoxItem();
                item.Content = i;
                hour_ComboBox.Items.Add(item);
            }

            for (int i = 1; i < 61; i++)
            {
                ComboBoxItem item = new ComboBoxItem();
                item.Content = i;
                min_ComboBox.Items.Add(item);
            }

            for (int i = 1; i < 61; i++)
            {
                ComboBoxItem item = new ComboBoxItem();
                item.Content = i;
                sec_ComboBox.Items.Add(item);
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void SubmitButton_Click(object sender, RoutedEventArgs e)
        {
            SocketHandler handler = new SocketHandler();
            T_HIPC_TIME_SET_REQ request = new T_HIPC_TIME_SET_REQ();
            request.head = new T_HIPC_NET_HEADER() { msgType = 0x8410 };
            if ((bool)rtc_RadioButton.IsChecked)
            {
                T_SYS_TIME time = new T_SYS_TIME();
                time.year = (ushort)datePicker.SelectedDate.Value.Year;
                time.month = (byte)datePicker.SelectedDate.Value.Month;
                time.day = (byte)datePicker.SelectedDate.Value.Day;

                if (String.IsNullOrEmpty(hour_ComboBox.Text))
                {
                    ComboBoxItem hour_ComboBoxItem = hour_ComboBox.SelectedItem as ComboBoxItem;
                    if (hour_ComboBoxItem == null)
                    {
                        ModernDialog.ShowMessage("请输入小时！", "信息", MessageBoxButton.OK);
                        return;
                    }
                    else
                    {
                        time.hour = (byte)Int32.Parse(hour_ComboBoxItem.Content.ToString());
                    }
                }
                else
                {
                    int result = 0;
                    if (Int32.TryParse(hour_ComboBox.Text, out result))
                    {
                        if (result >= 0 && result <= 24)
                        {
                            time.hour = (byte)result;
                        }
                        else
                        {
                            ModernDialog.ShowMessage("小时输入有误,请重新输入！", "错误", MessageBoxButton.OK);
                            return;
                        }
                    }
                    else
                    {
                        ModernDialog.ShowMessage("小时输入有误,请重新输入！", "错误", MessageBoxButton.OK);
                        return;
                    }
                }

                if (String.IsNullOrEmpty(min_ComboBox.Text))
                {
                    ComboBoxItem min_ComboBoxItem = min_ComboBox.SelectedItem as ComboBoxItem;
                    if (min_ComboBoxItem == null)
                    {
                        ModernDialog.ShowMessage("请输入分钟！", "信息", MessageBoxButton.OK);
                        return;
                    }
                    else
                    {
                        time.minute = (byte)Int32.Parse(min_ComboBoxItem.Content.ToString());
                    }
                }
                else
                {
                    int result = 0;
                    if (Int32.TryParse(min_ComboBox.Text, out result))
                    {
                        if (result >= 0 && result <= 24)
                        {
                            time.minute = (byte)result;
                        }
                        else
                        {
                            ModernDialog.ShowMessage("分钟输入有误,请重新输入！", "错误", MessageBoxButton.OK);
                            return;
                        }
                    }
                    else
                    {
                        ModernDialog.ShowMessage("分钟输入有误,请重新输入！", "错误", MessageBoxButton.OK);
                        return;
                    }
                }

                if (String.IsNullOrEmpty(sec_ComboBox.Text))
                {
                    ComboBoxItem sec_ComboBoxItem = sec_ComboBox.SelectedItem as ComboBoxItem;
                    if (sec_ComboBoxItem == null)
                    {
                        ModernDialog.ShowMessage("请输入秒！", "信息", MessageBoxButton.OK);
                        return;
                    }
                    else
                    {
                        time.second = (byte)Int32.Parse(sec_ComboBoxItem.Content.ToString());
                    }
                }
                else
                {
                    int result = 0;
                    if (Int32.TryParse(sec_ComboBox.Text, out result))
                    {
                        if (result >= 0 && result <= 24)
                        {
                            time.second = (byte)result;
                        }
                        else
                        {
                            ModernDialog.ShowMessage("秒输入有误,请重新输入！", "错误", MessageBoxButton.OK);
                            return;
                        }
                    }
                    else
                    {
                        ModernDialog.ShowMessage("秒输入有误,请重新输入！", "错误", MessageBoxButton.OK);
                        return;
                    }
                }

                request.time = time;
            }
            else
            {
                request.ntpEnable = 1;
                request.ntpServer = Encoding.ASCII.GetBytes(ip_TextBox.Text).Concat(new byte[20 - Encoding.ASCII.GetBytes(ip_TextBox.Text).Count()]).ToArray();
                int result = 0;
                if (Int32.TryParse(cycleSec_TextBox.Text, out result))
                {
                    request.cycleSec = (uint)result;
                }
                else
                {
                    ModernDialog.ShowMessage("间隔周期输入有误,请重新输入！", "错误", MessageBoxButton.OK);
                    return;
                }

                if (Int32.TryParse(port_TextBox.Text, out result))
                {
                    request.port = (ushort)result;
                }
                else
                {
                    ModernDialog.ShowMessage("端口输入有误,请重新输入！", "错误", MessageBoxButton.OK);
                    return;
                }
            }
            try
            {
                T_HIPC_TIME_SET_RESP response = handler.Handle<T_HIPC_TIME_SET_RESP>(request);
                if (response.head.errCode == 0)
                {
                    ModernDialog.ShowMessage("时间设置成功！", "信息", MessageBoxButton.OK);
                    Close();
                }
                else
                {
                    ModernDialog.ShowMessage(String.Format("时间设置失败！错误代码：", response.head.errCode), "错误", MessageBoxButton.OK);
                }
            }
            catch (Exception ex)
            {
                ModernDialog.ShowMessage(String.Format("时间设置失败！错误信息：", ex.Message), "错误", MessageBoxButton.OK);
            }
        }

        private void rtc_RadioButton_Click(object sender, RoutedEventArgs e)
        {
            rtc_Grid.Visibility = Visibility.Visible;
            ntp_Grid.Visibility = Visibility.Collapsed;
        }

        private void ntp_RadioButton_Click(object sender, RoutedEventArgs e)
        {
            rtc_Grid.Visibility = Visibility.Collapsed;
            ntp_Grid.Visibility = Visibility.Visible;
        }
    }
}
